package com.killer.view;

import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 监控信息类
 */
public class MonitorData {

    /**
     * 调用的方法名称
     */
    private String methodName;

    /**
     * 响应统计  key:响应毫秒数   value：对应的请求
     */
    private SortedMap<Long,List<RequestContext>> timeMap=new TreeMap<Long, List<RequestContext>>();

    /**
     * 总请求数
     */
    private int totalRequest;

    public MonitorData(String methodName, int totalRequest) {
        this.methodName=methodName;
        this.totalRequest=totalRequest;
    }

    /**
     * 统计tp耗时
     * @return
     */
    public long statTp(double rate){
        //计算达到这个tp的请求数(取整)
        int target=(int)(rate*totalRequest);
        //对列表进行按耗时时间从小到大排序
        List<Map.Entry<Long, List<RequestContext>>> entryList=getSortTimeMap();
        //然后逐项对比选出对应的耗时 这里已经确保了按key排序（从小到打)
        //统计到这里的个数
        int statCount=0;
        //最终的结果
        long responeTime=0l;
        SortedMap<Long,List<RequestContext>> removeRecords=new TreeMap<Long, List<RequestContext>>();
        for (Map.Entry<Long, List<RequestContext>> entry : entryList) {
            List<RequestContext> removeContexts = new ArrayList<RequestContext>();
            if (statCount > target) {
                //如果目标数已达统计个数，则这时候的响应时间即为这个tp值的响应时间
                return responeTime;
            }else{
                int count=0;
                for (RequestContext context : entry.getValue()) {
                    if (context.checkTimeOut()) {
                        //这里进行记录，待会需要移除
                        removeContexts.add(context);
                    }else{
                        //没超时则需要统计
                        count++;
                    }
                }
                //否则往下统计
                statCount=statCount+count;
                //同时记录对应的响应时间
                responeTime=entry.getKey();
                if (!CollectionUtils.isEmpty(removeContexts)) {
                    removeRecords.put(entry.getKey(), removeContexts);
                }
            }
        }
        return responeTime;
    }

    /**
     * 对map进行排序输出
     * @return
     */
    public List<Map.Entry<Long, List<RequestContext>>> getSortTimeMap(){
        List<Map.Entry<Long, List<RequestContext>>> list = new ArrayList<Map.Entry<Long, List<RequestContext>>>(timeMap.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Long, List<RequestContext>>>() {
            public int compare(Map.Entry<Long, List<RequestContext>> o1, Map.Entry<Long, List<RequestContext>> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        return list;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public SortedMap<Long, List<RequestContext>> getTimeMap() {
        return timeMap;
    }

    public void setTimeMap(SortedMap<Long, List<RequestContext>> timeMap) {
        this.timeMap = timeMap;
    }

    public int getTotalRequest() {
        return totalRequest;
    }

    public void setTotalRequest(int totalRequest) {
        this.totalRequest = totalRequest;
    }
}
